import cef
import log
import types
import util
PARENT_TYPEID = 0
PARENT_GROUPID = 1
PARENT_CATEGORYID = 2
PATTERN_CYCLIC = 0
PATTERN_RANDOM = 1
patternNames = {PATTERN_CYCLIC: 'Cyclic',
 PATTERN_RANDOM: 'Random'}

def GetSpawnPatterns():
    return patternNames.keys()



def GetSpawnPatternName(spawnPatternID):
    return patternNames.get(spawnPatternID, 'UNKNOWN')



def GetComponentName(componentID):
    componentView = cef.BaseComponentView.GetComponentClassByID(componentID)
    if (type(componentID) is str):
        return componentID
    else:
        if (componentView is None):
            log.LogTraceback(('UNKNOWN COMPONENT %s' % componentID))
            return ('UNKNOWN COMPONENT %s' % componentID)
        return componentView.__COMPONENT_CODE_NAME__



def GetParentTypeString(parentType):
    if (parentType == PARENT_TYPEID):
        return 'Type'
    else:
        if (parentType == PARENT_GROUPID):
            return 'Group'
        if (parentType == PARENT_CATEGORYID):
            return 'Category'
        return 'UNKNOWN'



def GetIngredientInitialValue(initValueRow):
    if (initValueRow.valueInt is not None):
        return initValueRow.valueInt
    else:
        if (initValueRow.valueFloat is not None):
            return initValueRow.valueFloat
        return initValueRow.valueString



def GetDBInitValuesFromValue(value):
    if (type(value) is types.IntType):
        return util.KeyVal(valueInt=value, valueFloat=None, valueString=None)
    else:
        if (type(value) is types.FloatType):
            return util.KeyVal(valueInt=None, valueFloat=value, valueString=None)
        if (type(value) is not types.StringType):
            try:
                value = str(value)
            except ValueError, e:
                return util.KeyVal(valueInt=None, valueFloat=None, valueString=None)
        try:
            valueInt = int(value)
        except ValueError, e:
            valueInt = None
        if (valueInt is not None):
            valueFloat = None
        else:
            try:
                valueFloat = float(value)
            except ValueError, e:
                valueFloat = None
        if ((valueInt is not None) or ((valueFloat is not None) or (value.strip() == ''))):
            value = None
        return util.KeyVal(valueInt=valueInt, valueFloat=valueFloat, valueString=value)


exports = util.AutoExports('entityCommon', locals())

